
package com.comtop.lcam.fwms.managementkey.socket;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * FIXME 类注释信息(此标记自动生成,注释填写完成后请删除)
 * 
 * <pre>
 * [
 * 调用关系:
 * 实现接口及父类:
 * 子类:
 * 内部类列表:
 * ]
 * </pre>
 * 
 * @author 作者
 * @since 1.0
 * @version 2018-11-27 作者
 */
public class EslcsSocketUtcUtil {
    
    private static final int DAY = 24 * 60 * 60;
    
    // 开始年份
    private static final int EPOCH = 2000;
    
    // 默认时间格式化字符串
    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * 判断是否为闰年
     * 
     * @param year
     * @return static
     */
    public static boolean isLeapYear(int year) {
        return ((year % 400) == 0 || (((year % 100) != 0) && ((year % 4) == 0))) ? true : false;
    }
    
    /**
     * 根据月份获取当月的天数
     * 
     * @param isLeapYear
     * @param month
     * @return static
     */
    public static int getDaysOfMonth(boolean isLeapYear, int month) {
        int days = 31;
        if (month == 1) {// 二月
            days = isLeapYear ? 29 : 28;
        } else {
            if (month > 6) {
                month--;
            }
            if ((month & 1) == 1) {
                days = 30;
            }
        }
        return days;
    }
    
    /**
     * 获取一年的天数
     * 
     * @param year
     * @return int
     */
    public static int getDaysOfYear(int year) {
        return isLeapYear(year) ? 366 : 365;
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param time
     * @return Date
     */
    public static Date convertFromUTCSeconds(long time) {
        long seconds = time % DAY;
        int second = (int) seconds % 60;
        int minute = (int) (seconds % 3600) / 60;
        int hour = (int) seconds / 3600;
        
        long days = time / DAY;
        int year = EPOCH;
        while (days >= getDaysOfYear(year)) {
            days -= getDaysOfYear(year);
            year++;
        }
        
        int month = 0;
        while (days >= getDaysOfMonth(isLeapYear(year), month)) {
            days -= getDaysOfMonth(isLeapYear(year), month);
            month++;
        }
        
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, (int) days + 1, hour, minute, second);
        
        return calendar.getTime();
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param time
     * @return long
     * @throws ParseException
     */
    public static long convertToUTCSeconds(String time) throws ParseException {
        return convertToUTCSeconds(time, PATTERN);
    }
    
    /**
     * 根据本地时间转换为UTC时间的秒数
     * 
     * @param time
     * @param pattern
     * @return long
     * @throws ParseException
     */
    public static long convertToUTCSeconds(String time, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = sdf.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int second = calendar.get(Calendar.SECOND);
        int minute = calendar.get(Calendar.MINUTE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        
        long seconds = hour * 60 * 60 + minute * 60 + second;
        
        // 获取当月完整的天数
        long days = day - 1;
        
        // 获取完整月份的天数
        while (--month >= 0) {
            days += getDaysOfMonth(isLeapYear(year), month);
        }
        
        // 获取完整年份的天数
        
        while (--year >= EPOCH) {
            days += getDaysOfYear(year);
        }
        return seconds + days * DAY;
    }
    
    /**
     * 获取当前BCD时间
     * 
     * @return String
     */
    public static String getNowDateBCD() {
        Date now = new Date();
        SimpleDateFormat f = new SimpleDateFormat("yyMMddHHmmss");
        return f.format(now);
    }
}
